<?php

namespace app\admin\controller;

use app\helper\Helper;
use app\model\AdminModel;
use app\model\DepartModel;
use app\model\RoleModel;
use app\service\AdminService;
use think\Db;
use think\Validate;

class Admin extends Base
{
    protected $service;

    protected $model;

    /**
     * Admin constructor.
     * @param AdminService $adminService
     */
    public function __construct(AdminService $adminService)
    {
        parent::__construct();
        $this->service = $adminService;
        $this->model = AdminModel::class;
    }

    /**
     * 管理员列表页面
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function adminList()
    {
        $info = DepartModel::departInfo();
        $role = RoleModel::getRoleInfo();
        $this->assign('info',$info);
        $this->assign('role',$role);
        return $this->fetch('admin/adminList');
    }

    /**
     * 添加 编辑页面
     * @return \think\response\View
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public function adminInfo()
    {
        $params = $this->request->param();
        $departInfo = DepartModel::departInfo();
        $role = RoleModel::getRoleInfo();
        $adminModel = new AdminModel();
        if(!empty($params['id'])){
            $info = $adminModel->where('id',$params['id'])->find();
            $this->assign('info',$info);
        }
        $this->assign('departInfo',$departInfo);
        $this->assign('role',$role);
        return $this->fetch('admin/adminInfo');
    }


    /**
     * 管理员列表请求接口
     * @return mixed|void
     */
    public function get()
    {
        $param = $this->request->param();
        $rule = [
            'admin_name|管理员名称'=>'max:10',
            'depart_id|部门'=>'number',
            'role_id|角色'=>'number',
        ];
        $validate = new Validate($rule);
        if(!$validate->check($param)){
            echo json_encode(['code'=>500,'msg'=>$validate->getError()]);die;
        }

        $page = isset($param['page']) ? intval($param['page']) : 1;
        $pageSize = isset($param['limit']) ? intval($param['limit']) : 10;

        $AdminModel = new AdminModel();
        $arr = ['like'=>'admin.admin_name','1'=>'admin.depart_id','2' => 'admin.role_id'];
        $where = $this->select_param($arr,$param);
        $list = $AdminModel->getAdminListPage($page,$pageSize,$where,false);
        $count = $AdminModel->getAdminListPage(0,0,$where,true);
       return ['code'=>0,'msg'=>'','count'=>$count,'data'=>$list];
    }


    /**
     * 更新|添加管理员信息
     */
    public function ApiSaveAdminInfo(){
        $param = $this->request->param();
        $rule = [
            'depart_id|部门'=>'require|number',
            'role_id|角色'=>'require|number',
            'admin_name|管理员名称'=>'require|max:20',
            'login_number|登录账号'=>'require',
            'phone|手机号'=>'require|/^[1][3,4,5,6,,7,8,9][0-9]{9}$/',
            'password|密码'=>'require',
            'status|状态'=>'require',
        ];
        $validate = new Validate($rule);
        if(!$validate->check($param)){
            echo json_encode(['code'=>400,'msg'=>$validate->getError()]);die;
        }
        $AdminModel = new AdminModel();
        $data=[
            'depart_id'=> $param['depart_id'],
            'role_id'=> $param['role_id'],
            'admin_name'=> $param['admin_name'],
            'login_number'=> $param['login_number'],
            'phone'=> $param['phone'],
            'status'=> $param['status'],
        ];
        if(!empty($param['id'])){
            $info = Db::name('admin')->field('password')->find($param['id']);
            if($info['password'] != $param['password']){
                $data['password'] = md5($param['password']);
            }
            $data['update_ts'] = afterTime();
            $res = $AdminModel->where('id',$param['id'])->update($data);
        }else{
            $data['password'] = md5($param['password']);
            $data['created_ts'] = afterTime();
            $res = $AdminModel->insert($data);
        }
        if($res!==false){
            echo json_encode(['code'=>200,'msg'=>'更新成功','data'=>$res]);die;
        }else{
            echo json_encode(['code'=>500,'msg'=>'error','data'=>$res]);die;
        }
    }

    /**
     * 删除管理员
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function deleteAdmin()
    {
        $param = $this->request->param();
        $res = Db::name('admin')->where('id',$param['id'])->update(['deleted'=>1]);
        if($res){
            echo json_encode(['code'=>200,'msg'=>'删除成功']);die;
        }else{
            echo json_encode(['code'=>400,'msg'=>'删除失败']);die;
        }
    }


}